import { NextFunction, Request, Response } from "express";

import Base from "../../common/controller/base";
import { checkField, encrypt, getUserInfo, trimStr } from "../../utils/common";
import commonUserService from '../../common/services/user'
import userService from './service'
/**
 * 用户
 */
class UserController extends Base {
    /**
     * 更改用户名
     * @param req 
     * @param res 
     */
    changeUserName = async (req: Request, res: Response, next: NextFunction) => {
        let { username } = req.body;

        // 校验参数
        if (!checkField(username)) return next({ msg: '参数不全', code: 400 })

        // 去掉两边空格
        username = trimStr(username)

        // 获取用户信息
        const info = getUserInfo(req)

        try {
            // 判断用户名是否已经存在了
            const isExist = await commonUserService.isUserExist(username);
            if (isExist) return next({ msg: '该用户名已经被使用了', code: 200 })

            // 更改
            await userService.updateUsername(info.id, username)
            this.success(res, { msg: '更改用户名成功', data: '' })
        } catch (e) {
            next({ msg: '服务器出错', code: 500 })
        }
    }

    /**
     * 更改密码
     * @param req 
     * @param res 
     */
    changePass = async (req: Request, res: Response, next: NextFunction) => {
        let { oldPassword: oldPass, newPassword: newPass } = req.body;

        // 校验参数
        if (!checkField(oldPass, newPass)) return next({ msg: '参数不全', code: 400 })

        // 去掉空格
        oldPass = trimStr(oldPass.toString())
        newPass = trimStr(newPass.toString())



        // 根据用户信息
        const info = getUserInfo(req);
        try {
            // 判断旧密码是否正确
            const isMatch = await userService.checkOldPass(info.id, encrypt(oldPass))
            if (!isMatch) return next({ msg: '旧密码与原来你的密码不匹配', code: 200 })

            // 判断新旧密码是否相同
            if (oldPass === newPass) return next({ msg: '新密码与旧密码不能相同', code: 200 })

            // 更新密码
            await userService.updatePass(info.id, encrypt(newPass))
            this.success(res, { msg: '修改密码成功', data: '' })
        } catch (e) {
            next({ msg: '服务器出错', code: 500 })
        }
    }


    /**
     * 获取用户信息
     * @param req 
     * @param res 
     */
    getUserInfo = async (req: Request, res: Response, next: NextFunction) => {
        const { id } = getUserInfo(req);
        try {
            const info = await userService.getUserInfoById(+id);

            this.success(res, { msg: '获取成功', data: info || {} })
        } catch (e) {
            next({ msg: '服务器出错', code: 500 })
        }
    }


    /**
     * 是否已经注册过了
     * @param req 
     * @param res 
     */
    isRegistered = async (req: Request, res: Response, next: NextFunction) => {
        const { username } = req.query;
        if (!checkField(username)) return next({ msg: '缺少参数', code: 400 })

        try {
            // 判断是否已经注册了
            const isExist = await commonUserService.isUserExist(username as string);
            if (isExist) return next({ msg: '该用户已经注册了', code: 200 })
            else return this.success(res, { msg: '该用户还没注册过', data: '' })
        } catch (e) {
            next({ msg: '服务器出错', code: 500 })
        }

    }

    /**
     * 判断token是否过期了
     * @param req 
     * @param res 
     */
    isTokenPass = async (_req: Request, res: Response) => {
        this.success(res, { msg: '登录未失效', data: '' })
    }

}

export default new UserController;
